package com.project.andeper.paperproject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wyk.greendaodemo.greendao.gen.User;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {

    //数据库服务类
    DBManager dbManager;

    @BindView(R.id.cancel)
    Button cancel;
    @BindView(R.id.tv_friend_title)
    TextView tvFriendTitle;
    @BindView(R.id.login_in)
    Button loginIn;
    @BindView(R.id.account)
    TextView account;
    @BindView(R.id.accountEdittext)
    EditText accountEdittext;
    @BindView(R.id.accountRel)
    RelativeLayout accountRel;
    @BindView(R.id.pwd)
    TextView pwd;
    @BindView(R.id.pwdEdittext)
    EditText pwdEdittext;
    @BindView(R.id.pwdRel)
    RelativeLayout pwdRel;
    @BindView(R.id.editRel)
    RelativeLayout editRel;
    @BindView(R.id.sound_help)
    TextView soundHelp;
    @BindView(R.id.sound_register)
    TextView soundRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        //requestWindowFeature(getWindow().FEATURE_CUSTOM_TITLE);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();


/*        dbManager = DBManager.getInstance(this);
            User user = new User();
            user.setId(Long.valueOf(188));
            user.setUsername("user"+"zzzx");
            user.setPassword("password"+"zzzx");
            dbManager.insertUser(user);*/

        //DBManager hdbManager = DBManager.getInstance(this);
    }

    @OnClick({R.id.cancel, R.id.login_in, R.id.sound_help, R.id.sound_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.login_in:
                dbManager = DBManager.getInstance(this);
                String userName = accountEdittext.getText().toString();
                String userPassword = pwdEdittext.getText().toString();
                List<User> users = dbManager.queryUserList(userName, userPassword);
                if (users.size() != 0) {
                    Toast.makeText(this, "恭喜！登陆成功！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                    this.finish();
                } else {
                    Toast.makeText(this, "用户名或密码错误！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.sound_help:
                Toast.makeText(this, "忘记密码还能怎么办呢？想什么呢！", Toast.LENGTH_SHORT).show();
                break;
            case R.id.sound_register:
                //Toast.makeText(this,"暂未开放注册",Toast.LENGTH_SHORT).show();
                LayoutInflater layoutInflater = LayoutInflater.from(LoginActivity.this);
                final View registerDialog = layoutInflater.inflate(R.layout.register_dialog, null);
                AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                AlertDialog alertDialog = builder
                        .setIcon(R.mipmap.register)
                        .setView(registerDialog)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dbManager = DBManager.getInstance(LoginActivity.this);
                                EditText editText = (EditText)registerDialog.findViewById(R.id.register_account);
                                String name = editText.getText().toString();
                                EditText editText1 = (EditText)registerDialog.findViewById(R.id.register_pwd);
                                String pwd = editText1.getText().toString();
                                User user = new User();
                                user.setUsername(name);
                                user.setPassword(pwd);
                                dbManager.insertUser(user);
                                Toast.makeText(LoginActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).create();

                alertDialog.show();
                break;
        }
    }
}